/*--------------------------------------------------------------------------
    This source file is part of Abraxas Game Engine
    For the latest info, see http://code.google.com/abraxasge/
    Copyright (C) 2009, Andrew LeRoy <aleroy@member.igda.org>
    ---

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.
--------------------------------------------------------------------------*/
#ifndef _Abx_Platform_hpp_
#define _Abx_Platform_hpp_

#include "AbxConfig.hpp"

namespace Abx
{
    // Initial platform/compiler-related stuff to set.
    #define ABX_PLATFORM_LINUX 1
    #define ABX_PLATFORM_WIN32 2
    #define ABX_PLATFORM_APPLE 3

    #define ABX_COMPILER_GNUC 1
    #define ABX_COMPILER_MSVC 2
    #define ABX_COMPILER_BORL 3

    #define ABX_ENDIAN_LITTLE 1
    #define ABX_ENDIAN_BIG 2

    #define ABX_ARCHITECTURE_32 1
    #define ABX_ARCHITECTURE_64 2

    // Finds the compiler type and version.
    #if defined( _MSC_VER )
    #   define ABX_COMPILER ABX_COMPILER_MSVC
    #   define ABX_COMP_VER _MSC_VER

    #elif defined( __GNUC__ )
    #   define ABX_COMPILER ABX_COMPILER_GNUC
    #   define ABX_COMP_VER (((__GNUC__)*100) + \
            (__GNUC_MINOR__*10) + \
            __GNUC_PATCHLEVEL__)

    #elif defined( __BORLANDC__ )
    #   define ABX_COMPILER ABX_COMPILER_BORL
    #   define ABX_COMP_VER __BCPLUSPLUS__
    #   define __FUNCTION__ __FUNC__ 
    #else
    #   pragma error "ERROR! Compiler not supported. DOH!@#!"

    #endif

    // See if we can use __forceinline or if we need to use __inline instead
    #if ABX_COMPILER == ABX_COMPILER_MSVC
    #   if ABX_COMP_VER >= 1200
    #       define FORCEINLINE __forceinline
    #   endif
    #elif defined(__MINGW32__)
    #   if !defined(FORCEINLINE)
    #       define FORCEINLINE __inline
    #   endif
    #else
    #   define FORCEINLINE __inline
    #endif
    
    // Define C calling convention qualifier
    #if ABX_COMPILER == ABX_COMPILER_MSVC
    #   define ABX_C_CALLING_QUALIFIER __cdecl
    #else
    #   define ABX_C_CALLING_QUALIFIER
    #endif

    // Finds the current platform
    #if defined( __WIN32__ ) || defined( _WIN32 )
    #   define ABX_PLATFORM ABX_PLATFORM_WIN32

    #elif defined( __APPLE_CC__)
    #   define ABX_PLATFORM ABX_PLATFORM_APPLE

    #else
    #   define ABX_PLATFORM ABX_PLATFORM_LINUX
    #endif

    // Find the arch type
    #if defined(__x86_64__) || defined(_M_X64) || defined(__powerpc64__) || defined(__alpha__) || defined(__ia64__) || defined(__s390__) || defined(__s390x__)
    #   define ABX_ARCH_TYPE ABX_ARCHITECTURE_64
    #else
    #   define ABX_ARCH_TYPE ABX_ARCHITECTURE_32
    #endif

    // For generating compiler warnings - should work on any compiler
    // As a side note, if you start your message with 'Warning: ', the MSVC
    // IDE actually does catch a warning :)
    #define ABX_QUOTE_INPLACE(x) # x
    #define ABX_QUOTE(x) ABX_QUOTE_INPLACE(x)
    #define ABX_WARN( x )  message( __FILE__ "(" QUOTE( __LINE__ ) ") : " x "\n" )

    //----------------------------------------------------------------------------
    // Windows Settings
    #if ABX_PLATFORM == ABX_PLATFORM_WIN32

        // If we're not including this from a client build, specify that the stuff
        // should get exported. Otherwise, import it.
        #   if defined( ABX_STATIC_LIB )
        #       define _AbxExport
        #       define _AbxPrivate
        #   else
        #       if defined( ABX_NONCLIENT_BUILD )
        #           define _AbxExport __declspec( dllexport )
        #       else
        #           if defined( __MINGW32__ )
        #               define _AbxExport
        #           else
        #               define _AbxExport __declspec( dllimport )
        #           endif
        #       endif
        #       define _AbxPrivate
        #   endif
        // Win32 compilers use _DEBUG for specifying debug builds.
        #   ifdef _DEBUG
        #       define ABX_DEBUG_MODE 1
        #   else
        #       define ABX_DEBUG_MODE 0
        #   endif

        // Disable unicode support on MingW at the moment, poorly supported in stdlibc++
        // STLPORT fixes this though so allow if found
        // MinGW C++ Toolkit supports unicode and sets the define __MINGW32_TOOLKIT_UNICODE__ in _mingw.h
        #if defined( __MINGW32__ ) && !defined(_STLPORT_VERSION)
        #   include<_mingw.h>
        #   if defined(__MINGW32_TOOLBOX_UNICODE__)
        #       define ABX_UNICODE_SUPPORT 1
        #   else
        #       define ABX_UNICODE_SUPPORT 0
        #   endif
        #else
        #   define ABX_UNICODE_SUPPORT 1
        #endif

    #endif
    //----------------------------------------------------------------------------

    //----------------------------------------------------------------------------
    // Linux/Apple Settings
    #if ABX_PLATFORM == ABX_PLATFORM_LINUX || ABX_PLATFORM == ABX_PLATFORM_APPLE

        // Enable GCC symbol visibility
        #   if defined( ABX_GCC_VISIBILITY )
        #       define _AbxExport  __attribute__ ((visibility("default")))
        #       define _AbxPrivate __attribute__ ((visibility("hidden")))
        #   else
        #       define _AbxExport
        #       define _AbxPrivate
        #   endif

        // A quick define to overcome different names for the same function
        #   define stricmp strcasecmp

        // Unlike the Win32 compilers, Linux compilers seem to use DEBUG for when
        // specifying a debug build.
        // (??? this is wrong, on Linux debug builds aren't marked in any way unless
        // you mark it yourself any way you like it -- zap ???)
        #   if defined( DEBUG ) || defined( _DEBUG )
        #       define ABX_DEBUG_MODE 1
        #   else
        #       define ABX_DEBUG_MODE 0
        #   endif

        // (Not sure if this actually needed--uncomment if necessary)
        //#if ABX_PLATFORM == ABX_PLATFORM_APPLE
        //    #define ABX_PLATFORM_LIB "AbxPlatform.bundle"
        //#else
        //    //OGRE_PLATFORM_LINUX
        //    #define ABX_PLATFORM_LIB "libAbxPlatform.so"
        //#endif

        // Always enable unicode support for the moment
        // Perhaps disable in old versions of gcc if necessary
        #define ABX_UNICODE_SUPPORT 1

    #endif

    //For apple, we always have a custom config.h file
    #if ABX_PLATFORM == ABX_PLATFORM_APPLE
    #    include "config.h"
    #endif

    //----------------------------------------------------------------------------

    //----------------------------------------------------------------------------
    // Endian Settings
    // check for BIG_ENDIAN config flag, set OGRE_ENDIAN correctly
    #ifdef ABX_CONFIG_BIG_ENDIAN
    #    define ABX_ENDIAN ABX_ENDIAN_BIG
    #else
    #    define ABX_ENDIAN ABX_ENDIAN_LITTLE
    #endif

    // Integer formats of fixed bit width
    typedef unsigned int uint32;
    typedef unsigned short uint16;
    typedef unsigned char uint8;
    // define uint64 type
    #if ABX_COMPILER == ABX_COMPILER_MSVC
        typedef unsigned __int64 uint64;
    #else
        typedef unsigned long long uint64;
    #endif

} // namespace abx

#endif // _Abx_Platform_hpp_
